Socket
Socket
Sign inDemoInstall

nunjucks

Package Overview
Dependencies
136
Maintainers
4
Versions
58
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)


Version published
Weekly downloads
645K
decreased by-9.25%
Maintainers
4
Install size
4.29 MB
Created
Weekly downloads
 

Package description

What is nunjucks?

Nunjucks is a powerful templating engine for JavaScript, inspired by Jinja2. It is designed to be fast, extendable, and easy to use, making it suitable for both server-side and client-side applications.

What are nunjucks's main functionalities?

Template Rendering

Nunjucks allows you to render templates with dynamic data. In this example, the template 'Hello {{ name }}!' is rendered with the context { name: 'World' }, resulting in 'Hello World!'.

const nunjucks = require('nunjucks');
nunjucks.configure({ autoescape: true });
const renderedString = nunjucks.renderString('Hello {{ name }}!', { name: 'World' });
console.log(renderedString);

Template Inheritance

Nunjucks supports template inheritance, allowing you to create a base template and extend it in other templates. This example shows a base template 'base.html' and an extending template 'index.html'. The rendered output will have the title 'Home' and the content 'Welcome to the homepage!'.

const nunjucks = require('nunjucks');
nunjucks.configure('views');
// base.html
// <html>
// <head><title>{% block title %}Default Title{% endblock %}</title></head>
// <body>{% block content %}{% endblock %}</body>
// </html>
// index.html
// {% extends 'base.html' %}
// {% block title %}Home{% endblock %}
// {% block content %}Welcome to the homepage!{% endblock %}
const renderedString = nunjucks.render('index.html');
console.log(renderedString);

Custom Filters

Nunjucks allows you to create custom filters to manipulate data within templates. In this example, a custom filter 'shorten' is created to truncate a string to a specified length. The template 'Result: {{ message | shorten(7) }}' will render as 'Result: Hello, ' when provided with the context { message: 'Hello, World!' }.

const nunjucks = require('nunjucks');
nunjucks.configure();
nunjucks.addFilter('shorten', function(str, count) {
  return str.slice(0, count || 5);
});
const renderedString = nunjucks.renderString('Result: {{ message | shorten(7) }}', { message: 'Hello, World!' });
console.log(renderedString);

Other packages similar to nunjucks

Readme

Source

Nunjucks

Nunjucks is a full featured templating engine for javascript. It is heavily inspired by jinja2. View the docs here.

Installation

npm install nunjucks

(View the CHANGELOG)

Documentation

See here.

Browser Support

Supported in all modern browsers. For IE8 support, use es5-shim.

Tests

Run the tests with npm test.

Watch master branch's tests running at http://mozilla.github.io/nunjucks/files/tests/browser/.

Mailing List

Join our mailing list and get help with and issues you have: https://groups.google.com/forum/?fromgroups#!forum/nunjucks

Contributors

https://github.com/mozilla/nunjucks/graphs/contributors

Keywords

FAQs

Last updated on 14 Sep 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc